﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNet.Utilities;
using YunHan.BuShi123.Model;
using YunHan.BuShi123.BLL;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Text;

public partial class admin_gathering_page : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Account_Model> AList = Account_BLL.GetModelList("top 5 *", "DateDiff(DAY, LastUpdateTime, GETDATE()) >= 1", "LastUpdateTime ASC");
        //List<Account_Model> AList = Account_BLL.GetModelList("top 5 *", "ID = 17", "LastUpdateTime ASC");
        if (AList.Count == 0)
        {
            Response.Write("{\"now\":\"今日所有记录均已采集完毕\",\"isend\":true}");
            Response.End();
        }

        StringBuilder Result = new StringBuilder();
        HttpHelper H = new HttpHelper();

        foreach (Account_Model A in AList)
        {
            string r = "";
            int NewNum = 0;
            List<Record_Model> RecordBatchAdd = new List<Record_Model>();
            try
            {
                #region Account
                HttpResult R = H.GetHtml(new HttpItem()
                {
                    URL = A.Url
                });

                string GetRecordListSQL = string.Format("AccountID IN ({0})", string.Join(",", AList.Select(a => a.ID.ToString()).ToArray()));
                List<Record_Model> RecordList = Record_BLL.GetModelList("YupooID", GetRecordListSQL, "");
               

                if (A.Url.Contains("v.yupoo.com"))
                {
                    MatchCollection AlbumsList = Regex.Matches(R.Html, "<div class=\\\"Sets\\\">.*?</p>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    foreach (Match item in AlbumsList)
                    {
                        int YuPooID = Convert.ToInt32(Regex.Match(item.Value, "(?<=/albums/).*?(?=/)", RegexOptions.Singleline | RegexOptions.IgnoreCase).Value);
                        //Record_Model Record = Record_BLL.GetModel("YupooID=@YupooID", new SqlParameter("@YupooID", YuPooID));
                        //if (Record != null)
                        //{
                        //    continue;
                        //}
                        if (RecordList.Exists(a => a.YupooID == YuPooID))
                        {
                            continue;
                        }

                        Record_Model Record = new Record_Model();
                        Record.ID = RecordBatchAdd.Count + 1;
                        Record.YupooID = YuPooID;
                        Record.AccountID = A.ID;
                        Record.Img = Regex.Match(item.Value, "(?<=src.*?=\\\").*?(?=\\\")", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value.Replace("photo.yupoo.com", "pic.yupoo.com").Replace("/square.jpg", "/small.jpg");
                        Record.Name = A.Name;
                        Record.QQ = A.QQ;
                        Record.Title = Regex.Match(item.Value, "(?<=alt=\\\").*?(?=\\\")", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value;
                        Record.Url = "http://v.yupoo.com" + Regex.Match(item.Value, "(?<=href=\\\").*?(?=\\\")", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value;
                        Record.AccountURL = A.Url;
                        //Record_BLL.Add(Record);
                        RecordBatchAdd.Add(Record);
                        NewNum++;
                    }
                }
                else
                {
                    string AllPageHtml = R.Html;

                    try
                    {
                        int PageCount = Convert.ToInt32(Regex.Match(R.Html, "(?<=>共).*?(?=页<)", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value);
                        for (int i = 2; i <= PageCount; i++)
                        {
                            R = H.GetHtml(new HttpItem()
                            {
                                URL = A.Url + "&page=" + i.ToString()
                            });
                            AllPageHtml += R.Html;
                        }
                    }
                    catch (Exception)
                    {

                    }


                    MatchCollection AlbumsList = Regex.Matches(AllPageHtml, "<div class=\\\"showindex__children\\\">.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    foreach (Match item in AlbumsList)
                    {
                        int YuPooID = Convert.ToInt32(Regex.Match(item.Value, "(?<=/albums/)\\d+", RegexOptions.Singleline | RegexOptions.IgnoreCase).Value);
                        if (RecordList.Exists(a => a.YupooID == YuPooID))
                        {
                            continue;
                        }

                        Record_Model Record = new Record_Model();
                        Record.ID = RecordBatchAdd.Count + 1;
                        Record.YupooID = YuPooID;
                        Record.AccountID = A.ID;
                        Record.Img = "http:" + Regex.Match(item.Value, "(?<=src.*?=\\\").*?(?=\\\")", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value.Replace("photo.yupoo.com", "pic.yupoo.com");
                        Record.Name = A.Name;
                        Record.QQ = A.QQ;
                        Record.Title = Regex.Match(item.Value, "(?<=title=\\\").*?(?=\\\")", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value;
                        Record.Url = Regex.Match(item.Value, "(?<=href=\\\").*?(?=\\\")", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value;
                        if (Record.Url.StartsWith("//x.yupoo.com"))
                        {
                            Record.Url = "http:" + Record.Url;
                        }
                        else
                        {
                            Record.Url = "http://x.yupoo.com" + Record.Url;
                        }
                        Record.AccountURL = A.Url;
                        //Record_BLL.Add(Record);
                        RecordBatchAdd.Add(Record);
                        NewNum++;
                    }
                }

                RecordBatchAdd = RecordBatchAdd.OrderByDescending(a => a.ID).ToList();
                Record_BLL.BatchAdd(RecordBatchAdd, 1024);
                #endregion

                r = NewNum.ToString();
            }
            catch (Exception ex)
            {
                //Result.Append(A.Name + "：" + NewNum.ToString() + "，");
                //foreach (var item in RecordBatchAdd)
                //{
                //    try
                //    {
                //        item.ID = Record_BLL.Add(item);
                //    }
                //    catch (Exception err)
                //    {
                //        throw;
                //    }
                //}
                r = ex.Message;
            }

            Account_BLL.Update("LastUpdateTime=GETDATE()", "ID=@ID", A);

            Result.Append(A.Name + "：" + r + "，");
        }

        string result = "{\"now\":\"" + Result.ToString() + "\",\"isend\":false}";
        Response.Write(result);
        Response.End();
    }
}